$(function(){
	table.init();
});
var table=function(){
	var mmg;
	var storage=JSON.parse(localStorage.templateBuilder);
	var init=function(){
		//刚进页面的时候，storage.selectedTable没有值
		storage.selectedTable='';
		_loadData();
		_initGrid();
		_eventRegister();
	};
	var _eventRegister=function(){
		// console.log(getUrlParam('mode'));
		// $('.panel-body').find('input').removeAttr('checked');
		//将前一个页面传过来的origin存储在本地存储中
		storage.origin=getUrlParam('origin');
		$('#pre').attr('href',$('#pre').attr('href')+'?mode='+storage.mode);
		// //单选框选中事件
		// $('.panel-body').on('click','input',function(e){
		// 	storage.selectedTable=$(this).attr('data-table');
		// });

		//下一步按钮的监听事件，如果选择了表，可以跳到下一步，如果没有，不能跳到下一步
		$('#next').on('mousedown',function(e){
			var selectedRows=mmg.selectedRows();
			if(selectedRows.length==0){
				alert('请选择表！');
			}else{
				var selectedRow=selectedRows[0];
				storage.selectedTable=selectedRow.tablename;
				localStorage.templateBuilder=JSON.stringify(storage);
			}
		});

		mmg.on('cellSelected',function(e, item, rowIndex, colIndex){
			console.log('cellSelected');
			console.log('rowIndex:'+rowIndex+';colIndex:'+colIndex);
			console.log(item);
		});
		
	};
	//mmGrid生成代码
	var _initGrid=function(){
		//测试数据
		var items=[
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'afJob',
				comment:'JOB表',
				remark:'备注',
				type:'TABLE',
				tablename:'AF_JOB',
				tablecomment:'JOB表'
			},
			{
				name:'efBill',
				comment:'账单表',
				remark:'备注',
				type:'TABLE',
				tablename:'EF_BILL',
				tablecomment:'账单表'
			},
			{
				name:'eoOrder',
				comment:'物流订单表',
				remark:'备注',
				type:'TABLE',
				tablename:'EO_ORDER',
				tablecomment:'物流订单表'
			},
			{
				name:'eoOrderFee',
				comment:'订单费用表',
				remark:'备注',
				type:'TABLE',
				tablename:'EO_ORDER_FEE',
				tablecomment:'订单费用表'
			},
			{
				name:'esAccount',
				comment:'账户表',
				remark:'备注',
				type:'TABLE',
				tablename:'ES_ACCOUNT',
				tablecomment:'账户表'
			},
			{
				name:'esRole',
				comment:'角色表',
				remark:'备注',
				type:'TABLE',
				tablename:'ES_ROLE',
				tablecomment:'角色表'
			}
		];
		//表头
		var cols=[
			// {title:'NAME', name:'name' ,width:100, sortable: true, type:'string', align:'left'},
			// {title:'COMMENT', name:'comment' ,width:130, sortable: true, type:'string', align:'left'},
			// {title:'REMARK', name:'remark' ,width:100, sortable: true, type:'string', align:'left'},
			
			{title:'表名', name:'tablename' ,width:120, sortable: true, type:'string', align:'left'},
			{title:'表注释', name:'tablecomment' ,width:150, sortable: true, type:'string', align:'left'},
			{title:'类型', name:'type' ,width:80, sortable: true, type:'string', align:'left'}
		];
		mmg=$('#grid').mmGrid({
			width:'auto',
			height: 450,
			cols: cols, 
            items: items,
            checkCol:true,
            fullWidthRows:true,
            nowrap : true
		});
	};
	var _loadData=function(){
		setTimeout(function(){
			$('.panel-body').find('.spinner').addClass('hide')
				.next().removeClass('hide');
		},3000);
	};
	return{
		init:init
	};
}();
//获取地址栏中传过来的参数
function getUrlParam(key) {
	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
};